/*
  Copyright (C) 2017 Open Intelligent Machines Co.,Ltd

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>


#include <iosfwd>
#include <memory>
#include <utility>
#include "sys/stat.h"
#include <dirent.h>
#include<sstream>
#include <dirent.h>

#include <opencv2/opencv.hpp>
#include "mxnet_predict.h"
using namespace std;
using namespace cv;

#include <sys/time.h>
unsigned long get_cur_time(void) {
  struct timeval tv;
  unsigned long ts;

  gettimeofday(&tv, NULL);

  ts = tv.tv_sec * 1000000 + tv.tv_usec;  
  return ts;
}

void getAllFiles(string path, std::vector<std::string>& files)
{
        DIR *dp;
        struct dirent *dirp;
        if((dp = opendir(path.c_str())) == NULL)
        {
            cout << "Can't open " <<path<< endl;
        }
        while((dirp = readdir(dp)) != NULL)
        {
            // cout << dirp->d_name << endl;
            string s(dirp->d_name);
            if(s != "." && s !="..")
              files.push_back(s);
        }
        closedir(dp); 
}


void split(std::string& s, std::string& delim,std::vector< std::string >& ret)
{
	size_t last = 0;
	size_t index=s.find_first_of(delim,last);
	while (index!=std::string::npos)
	{
		ret.push_back(s.substr(last,index-last));
		last=index+1;
		index=s.find_first_of(delim,last);
	}
	if (index-last>0)
	{
		ret.push_back(s.substr(last,index-last));
	}
}
int main(int argc, char * argv[])
{
    string model_param,model_json;
    model_param = "/data/gcy/nangang/vehicle_reid/onnx/tutorials-master/tutorials/test-0001.params"; 
    model_json = "/data/gcy/nangang/vehicle_reid/onnx/tutorials-master/tutorials/test-symbol.json"; 
    
    //使用0号显卡
    int ngpuid=0;
    int batch=1;
    //网络结构输入层通道数
    int num_channels_=1;
    //网络结构输入层尺寸
    int input_h=1080;
    int input_w=1920;
    //网络结构输入层名称
    string input_name="1";
    mxnet_predict predictor(model_param,model_json,\
        batch,num_channels_,input_h,input_w,input_name,ngpuid);
    

    double ftick, etick;
    double ticksPerUs = cv::getTickFrequency() / 1000000;    
    Mat frame=imread("/data/HuNan_lvchao/videos-310morning/8_pics/vlcsnap-2018-03-21-10h08m47s681.png",0);
    for(int i=0;i<10000;i++)
    {
        ftick = get_cur_time();
        Mat img_clone=frame.clone();
        Rect rect = Rect(100,100,800,800);
        size_t nocunt=predictor.run(img_clone , rect);
        etick = get_cur_time();
        cout<<"feat:"<<endl;
        for (int i=0;i<1;i++)
        {
            cout<<"总人数："<<nocunt<<" ";
        }
        cout<<endl;

        std::cout<<"total time: "<<etick - ftick<<" us"<<std::endl;
    }

    

    //cv::imshow("img", frame);
    //cv::waitKey(0);
cout<<"=======END======"<<endl;
    return 0;
}
